<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :inline="true" :model="filters">
				<el-form-item>
					<el-input v-model="filters.name" placeholder="姓名"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="keywordQuery">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="handleAdd">新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表-->
    <el-table :data="departments" highlight-current-row v-loading="listLoading"
              @selection-change="selsChange" style="width: 100%;" @cell-click="table = true">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" label="序号" width="60">
      </el-table-column>
      <el-table-column prop="id" label="id" width="100" sortable>
      </el-table-column>
      <el-table-column prop="sn" label="字典编号" width="100" sortable>
      </el-table-column>
      <el-table-column prop="name" label="字典名称" width="100" sortable>
      </el-table-column>
<!--      <el-table-column prop="state" label="员工状态" width="100" :formatter="formatState" sortable>
      </el-table-column>-->
<!--      <el-table-column prop="state" label="员工状态" width="100" sortable>
        <template slot-scope="scope">
          <span style="color: green" v-if="scope.row.state==1">正常</span>
          <span style="color: red" v-else-if="scope.row.state==0">禁用</span>
          <span style="color: #fff200" v-else-if="scope.row.state==2">待用</span>
          <span style="color: #00ffd9" v-else-if="scope.row.state==3">观察</span>
          <span style="color: darkslategray" v-else>未知</span>
        </template>
      </el-table-column>-->
      <el-table-column label="操作">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index,scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index,scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
			<el-pagination
          layout="prev, pager, next"
          @current-change="handleCurrentChange"
          :page-size="pageSize"
          :total="totals"
          :current-page="currentPage"
          style="float:right;">
			</el-pagination>
		</el-col>

		<!--编辑界面-->
		<el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
			<el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
<!--	不能加这个			<el-form-item label="员工ID" prop="id">
					<el-input v-model="editForm.id" auto-complete="off"></el-input>
				</el-form-item>-->
        <el-form-item label="字典编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典名称" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
      <!--        <el-form-item label="商店时间" prop="email">
          <el-input v-model="editForm.registerTime" auto-complete="off"></el-input>
        </el-form-item>-->


			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="editFormVisible = false">取消</el-button>
				<el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
			</div>
		</el-dialog>

    <!--    抽屉-->
<!--    <el-button type="button" @click="table = true">抽屉
    </el-button>-->
    <el-drawer
        title="类型明细表抽屉"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table :data="gridData">
        <el-table-column property="id" label="类型编号" width="200"></el-table-column>
        <el-table-column property="name" label="类型名称"></el-table-column>
      </el-table>
    </el-drawer>
	</section>
</template>

<script>

	export default {
		data() {
			return {
        table: false,
        dialog: false,
        loading: false,
        gridData: [],

        deptTree:[],
        employees: [],
        title:"",
				filters: {
					name: ''
				},
				departments: [],
				totals: 0,
        currentPage: 1,
        pageSize: 8,
				listLoading: false,
				sels: [],//列表选中列

				editFormVisible: false,//编辑界面是否显示
				editLoading: false,
        //表单校验规则
				editFormRules: {
					name: [
						{ required: true, message: '请输入商店名称', trigger: 'blur' }
					],
          tel: [
            { required: true, message: '请输入手机号', trigger: 'blur' }
          ]
				},
				//编辑界面数据
				editForm: {
					id: null,
          sn: '',
          name:''
				},

			}
		},
		methods: {
      //员工回显
      getEmployees(){
        this.$http.get("/employee")
        .then(res=>{
          this.employees=res.data
        }).catch(res=>{

        })
      },
      //关键字查询
      keywordQuery(){
        this.currentPage = 1;
        this.getDepartments();
      },
			//性别显示转换
			formatSex: function (row, column) {
				return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
			},
      //分页
			handleCurrentChange(currentPage) {
				this.currentPage = currentPage;
				this.getDepartments();
			},
			//获取用户列表
			getDepartments() {
				let para = {
          currentPage: this.currentPage,
          pageSize: this.pageSize,
          keywords: this.filters.name
				};
        //开启忙等框
				this.listLoading = true;
				//NProgress.start();
				this.$http.post("/systemdictionarytype",para)
        .then(res=>{
          //给totals赋值 - 计算分页栏
          this.totals = res.data.totals;
          //赋值分页数据 - 显示当前页的分页数据
          this.departments = res.data.data;
          //隐藏加载框/忙等框
          this.listLoading = false;
        }).catch(res=>{
            this.$message.error("系统错误，稍后重试")
        })
			},
			//删除
			handleDel: function (index, row) {
				this.$confirm('确认删除该记录吗?', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					this.$http.delete("/systemdictionarytype/"+row.id)
          .then(res=>{
            this.listLoading = false;//关闭忙等框
            if(res.data.success){
              this.$message({
                message:'删除成功',
                type:'success'
              })
            }else {
              this.$message({
                message:res.data.msg,
                type:'error'
              })
            }
            //重新发送请求，刷新页面
            this.currentPage=1;
            this.getDepartments();
          }).catch(res=>{
            this.$message({
              message:'系统繁忙，稍后重试',
              type:'error'
            })
          })

				}).catch(() => {

				});
			},
			//显示编辑界面
			handleEdit: function (index, row) {
        this.getEmployees();
        this.title="编辑";
				this.editFormVisible = true;
				this.editForm = Object.assign({}, row);
			},
			//显示新增界面
			handleAdd: function () {
        this.getEmployees();
        this.title="新增";
				this.editFormVisible = true;//弹出模态框
        //清空模态框
				this.editForm = {
          id: null,
          username: '',
          phone: '',
          email: '',
          salt: '',
          password: '',
          age: null,
          department_id: '',
          state: 0,

        };
			},
			//编辑
			editSubmit: function () {
        //表单校验
				this.$refs.editForm.validate((valid) => {
					if (valid) {
						this.$confirm('确认提交吗？', '温馨提示', {}).
            then(() => {
              //开启了盲等框
							this.editLoading = true;
              //克隆数据
							let para = Object.assign({}, this.editForm);
							this.$http.put("/systemdictionarytype",para)
              .then(res=>{
                this.editLoading = false; //关闭忙等框
                if(res.data.success){
                  this.$message({
                    message: '操作成功',
                    type: 'success'
                  });
                }else {
                  this.$message({
                    message: res.data.msg,
                    type: 'error'
                  });
                }
                //关闭模态框
                this.editFormVisible=false;
                //刷新页面
                this.getDepartments();
              }).catch(res=>{
                this.$message({
                  message: '系统异常，稍后重试',
                  type: 'error'
                });
              })
						});
					}
				});
			},

			selsChange: function (sels) {
				this.sels = sels;
			},
			//批量删除
			batchRemove: function () {
				var ids = this.sels.map(item => item.id);
				this.$confirm('确认删除选中记录吗？', '提示', {
					type: 'warning'
				}).then(() => {
					this.listLoading = true;
					//NProgress.start();
					//let para = { ids: ids };
          this.$http.patch("/systemdictionarytype",ids)
          .then(res=>{
            this.listLoading = false;
            if(res.data.success){
              this.$message({
                message: '删除成功',
                type: 'success'
              });
            }else {
              this.$message({
                message: res.data.msg,
                type: 'error'
              });
            }
            this.getDepartments();
          }).catch(res=>{
            this.$message({
              message:'系统繁忙，稍后重试',
              type:'error'
            })
          })
					/*batchRemoveDepartment(para).then((res) => {
						this.listLoading = false;
						//NProgress.done();
						this.$message({
							message: '删除成功',
							type: 'success'
						});
						this.getDepartments();
					});*/
				}).catch(() => {

				});
			},
      //11.抽屉
      getGridData(){
        this.$http.get("/systemdictionarydetail").then(res => {
          this.gridData = res.data;
        })
      }
		},
		mounted() {
			this.getDepartments();
      this.getGridData();
		}
	}

</script>

<style scoped>

</style>